from django.db import models
from django.contrib.auth.models import User
import datetime
from django.utils import timezone

"""
@author: Daniel Geymayer, Alexander Fruehwirth, Michael Joergler
@version: 1.0
"""

DIFFICULTIES = (
        (u'easy', u'easy'),
        (u'normal', u'normal'),
        (u'hard', u'hard'),
)

VEGETARIAN = (
        (u'non-vegetarian', u'non-vegetarian'),
        (u'vegetarian', u'vegetarian'),
        (u'both', u'both'),
)

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True, editable=False)

    def __unicode__(self):
        return self.user.get_full_name()

class Recipe(models.Model):
    """A recipe"""
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField(verbose_name='date published', default=timezone.now(), blank=True, editable=False)
    servings = models.PositiveIntegerField()
    kcal = models.PositiveIntegerField(null=True, blank=True)
    difficulty = models.CharField(max_length=50, choices=DIFFICULTIES)
    vegetarian = models.CharField(max_length=50, choices=VEGETARIAN)
    description = models.TextField()
    user = models.ForeignKey(User, editable=False)
    
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    
    def get_ingredients(self):
        return Recipe_Ingredient.objects.filter(recipe=self)
    
    def __unicode__(self):
        return self.title

class Recipe_Ingredient(models.Model):
    """An ingredient used in a specific recipe"""
    recipe = models.ForeignKey(Recipe, editable=False)
    name = models.CharField(max_length=200)
    amount = models.PositiveIntegerField()
    unit = models.CharField(max_length=200)
    def __unicode__(self):
        return "%s %s %s" % (self.amount, self.unit, self.name)

class Filter(models.Model):
    """Filter vegetarian, difficulty, ingredients, """
    user = models.ForeignKey(User, editable=False)
    difficulty = models.CharField(max_length=50, choices=DIFFICULTIES, blank=True, null=True)
    vegetarian = models.CharField(max_length=50, choices=VEGETARIAN, blank=True, null=True)
    active = models.BooleanField(default=False)

class Ingredient_Filter(models.Model):
    """Filter vegetarian, difficulty, ingredients, """
    filter = models.ForeignKey(Filter, editable=False)
    ingredient = models.CharField(max_length=200)
    exclude = models.BooleanField(default=False)

class Rating(models.Model):
    """A rating for a recipe by a user"""
    recipe = models.ForeignKey(Recipe, editable=False)
    user = models.ForeignKey(User, editable=False)
    rating = models.PositiveIntegerField()
    
    def __unicode__(self):
        return "%s / 5" % (self.rating)
    
    class Meta:
        unique_together = ('recipe', 'user')

class Picture(models.Model):
    """Picture belonging to a recipe"""
    recipe = models.ForeignKey(Recipe, editable=False)
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    url = models.CharField(max_length=255)
    
    def __unicode__(self):
        return "%s: %s" % (self.name, self.description)
